package com.a3dweijing.wetouch2.Util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;

import static android.content.pm.PackageManager.PERMISSION_DENIED;

/**
 * Created by Administrator on 2018/3/22.
 * 6.0动态申请权限
 * 需要重写onRequestPermissionsResult方法回调
 * PERMISSION_GRANTED同意
 * PERMISSION_DENIED拒绝
 * 23api才需要
 */

public class PermissionapplicationHelp {

    public static int QUANXIANCODE = 235;

    public static int Permissionapplication(Activity activity, String INSPECT, String[] PERMISSIONSNAME) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                //检测是否有写的权限
                int permission = ActivityCompat.checkSelfPermission(activity,
                        INSPECT);
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    ActivityCompat.requestPermissions(activity, PERMISSIONSNAME, QUANXIANCODE);
                }
                return permission;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return PERMISSION_DENIED;
        } else
            return PackageManager.PERMISSION_GRANTED;
    }

    public static int Permissionapplication(Fragment fragment, String INSPECT, String[] PERMISSIONSNAME) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                //检测是否有写的权限
                int permission = ActivityCompat.checkSelfPermission(fragment.getContext(),
                        INSPECT);
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    fragment.requestPermissions(PERMISSIONSNAME, QUANXIANCODE);
                }
                return permission;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return PERMISSION_DENIED;
        } else
            return PackageManager.PERMISSION_GRANTED;
    }

}
